// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"log"
	"unsafe"

	"strings"

	. "github.com/CodyGuo/win"
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
)

type MyMainWindow struct {
	*walk.MainWindow
}

func main() {
	var le *walk.LineEdit
	var wv *walk.WebView
	mw := new(MyMainWindow)

	// We load our icon from a file.
	icon, err := walk.NewIconFromFile("x.ico")
	if err != nil {
		log.Fatal(err)
	}

	// Create the notify icon and make sure we clean it up on exit.
	ni, err := walk.NewNotifyIcon()
	if err != nil {
		log.Fatal(err)
	}
	defer ni.Dispose()

	// Set the icon and a tool tip text.
	if err := ni.SetIcon(icon); err != nil {
		log.Fatal(err)
	}
	if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
		log.Fatal(err)
	}

	// When the left mouse button is pressed, bring up our balloon.
	ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
		if button != walk.LeftButton {
			return
		}

		if err := ni.ShowCustom(
			"Walk NotifyIcon Example",
			"There are multiple ShowX methods sporting different icons."); err != nil {

			log.Fatal(err)
		}
	})

	// We put an exit action into the context menu.
	openAction := walk.NewAction()
	if err := openAction.SetText("O&pen"); err != nil {
		log.Fatal(err)
	}
	openAction.Triggered().Attach(func() {
		log.Println("打开主窗口！")

		//walk.App().Exit(0)
	})
	if err := ni.ContextMenu().Actions().Add(openAction); err != nil {
		log.Fatal(err)
	}

	// We put an exit action into the context menu.
	exitAction := walk.NewAction()
	if err := exitAction.SetText("E&xit"); err != nil {
		log.Fatal(err)
	}
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
		log.Fatal(err)
	}

	// The notify icon is hidden initially, so we have to make it visible.
	if err := ni.SetVisible(true); err != nil {
		log.Fatal(err)
	}

	// Now that the icon is visible, we can bring up an info balloon.
	if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
		log.Fatal(err)
	}

	if err := (MainWindow{
		AssignTo: &mw.MainWindow,
		Icon:     Bind("'../img/' + icon(wv.URL) + '.ico'"),
		Title:    "华中师范大学多媒体教学使用满意度调查",
		MinSize:  Size{800, 600},
		Layout:   VBox{MarginsZero: true},
		Children: []Widget{
			LineEdit{
				AssignTo: &le,
				Text:     Bind("wv.URL"),
				OnKeyDown: func(key walk.Key) {
					if key == walk.KeyReturn {
						wv.SetURL(le.Text())
					}
				},
			},
			WebView{
				AssignTo:     &wv,
				Name:         "wv",
				URL:          "http://127.0.0.1:8080/myddc",
				OnURLChanged: t1, //mw.t2, //响应webview的URL改变事件
			},
		},
		Functions: map[string]func(args ...interface{}) (interface{}, error){
			"icon": func(args ...interface{}) (interface{}, error) {
				if strings.HasPrefix(args[0].(string), "https") {
					return "check", nil
				}

				return "stop", nil
			},
		},
	}.Create()); err != nil {
		log.Fatal(err)
	}
	mw.Run()
}

func (mw *MyMainWindow) t2() {
	walk.MsgBox(mw, "Change View", "By now you may have guessed it. Nothing changed.", walk.MsgBoxIconInformation)
}

func t1() {
	log.Println("刷新了网页事件！")
	shutdown()
}

func shutdown() {
	//关机
	var (
		hToken       HANDLE
		tkp          TOKEN_PRIVILEGES
		lpSystemName *byte
		lpName       *byte
	)
	//tmp := []byte("SeSecurityPrivilege")
	//lpName = &tmp[0]
	lpName = StringToBytePtr(SE_SHUTDOWN_NAME)
	lpSystemName = nil
	OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)
	LookupPrivilegeValueA(lpSystemName, lpName, &tkp.Privileges[0].Luid)
	tkp.PrivilegeCount = 1 // one privilege to set
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED
	//AdjustTokenPrivileges(hToken, false, &tkp, 0, &tkp, &ReturnLength)
	AdjustTokenPrivileges(hToken, false, &tkp, unsafe.Sizeof(tkp), nil, nil)
	ret2 := GetLastError()
	if ret2 != ERROR_SUCCESS {
		log.Println("提升进程权限失败！")
		return
	} else {
		log.Println("提升进程权限成功！")
		//ret := ExitWindowsEx(EWX_POWEROFF|EWX_FORCE, 0x00000001)
		ret := true
		ret2 := GetLastError()
		if ret == false {
			log.Println("关机失败！错误代码：", ret2)
			return
		} else {
			log.Println("关机执行成功！")
			return
		}

	}
}
